/*******************************************************************************
 * Copyright (c) 2011-11-10 @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>.
 * All rights reserved.
 *
 * Contributors:
 *     <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> - initial API and implementation
 ******************************************************************************/
package com.redhat.auth.ejb.impl;

import javax.ejb.Remote;
import javax.ejb.Stateless;

import org.springframework.stereotype.Service;

import com.redhat.auth.domain.BizSystem;
import com.redhat.auth.ejb.BizSystemApplication;
import com.redhat.auth.ejb.vo.BizSystemVO;

/**
 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> 
 * @since 2011-11-10
 */
@Service("ramsBizSystemApplication")
@Stateless(name = "BizSystemApplicationBean")
@Remote(BizSystemApplication.class)
@javax.interceptor.Interceptors(SpringEJBIntercepter.class)
public class BizSystemApplicationImpl implements BizSystemApplication {

	public void removeBizSystem(Long bizSystemId) {
		BizSystem system = BizSystem.get(bizSystemId);
		if (system != null) {
			system.remove();
		}
	}

	public BizSystemVO createBizSystem(BizSystemVO vo) {
		BizSystem system = Convertor.vo2po(vo, new BizSystem());
		system.create();
		return Convertor.po2vo(system, null);
	}

	public BizSystemVO updateBizSystem(BizSystemVO vo) {
		BizSystem system = BizSystem.get(vo.getId());
		if (system == null) {
			return null;
		}
		{
			if (vo.getDescription() != null) {
				system.setDescription(vo.getDescription());
			}
			//system.setName(null);
			//system.setRegistryDate(null);
			//system.setSerialNumber(null);
			if (vo.getStatus() == BizSystem.Status.ENABLED.ordinal()) {
				system.setStatus(BizSystem.Status.ENABLED);
			} else if (vo.getStatus() == BizSystem.Status.DISABLED.ordinal()) {
				system.setStatus(BizSystem.Status.DISABLED);
			}
			if (vo.getUrl() != null) {
				system.setUrl(vo.getUrl());
			}
		}
		system.save();
		return Convertor.po2vo(system, null);
	}

}
